﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using TMDT_Nhom1A.DTO;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.utils;
using System.Collections.Generic;

namespace TMDT_Nhom1A.Service
{
    /// <summary>
    /// Summary description for CouponWebservice
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CouponWebservice : System.Web.Services.WebService
    {



        [WebMethod]
        public string Authenticate(string username, string password)
        {
            string sid = "";

            sid = CouponWebservice_BUS.Authenticate(username, password);

            if (sid.Equals("1"))
            {
                return ErrorCode.NotYetActiveAccountERR;
            }
            else
            {
                if (sid.Equals("2"))
                    return ErrorCode.AccountIsClockedERR;
                else
                    if (sid.Equals("3"))
                        return ErrorCode.NotYetRegistryUseWebserviceERR;
                    else
                        if (sid.Equals("4"))
                            return ErrorCode.UsernamePasswordOrNotActiveOrNotyetRegisterWSERR;
                        else
                            if (sid.Equals("5"))
                                return ErrorCode.UnSuccessfulAuthenticateMsg;
                            else
                            {
                                return sid;
                            }
            }
        }

        [WebMethod]
        public Coupon[] FindAllCoupons(string sid)
        {
            //get host name;
            string path = "";
            HttpContext context = HttpContext.Current;
            path = String.Format("{0}://{1}/images/", context.Request.Url.Scheme, context.Request.Url.Host);

            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {
                return CouponWebservice_BUS.FindAllCoupons(path);
            }

            return null;
        }

        [WebMethod]
        public Coupon[] FindCoupons(string sid, string productName,
                           DateTime startDate, DateTime endDate,
                           int minPrice, int maxPrice)
        {
            //get host name
            string path = "";
            HttpContext context = HttpContext.Current;
            path = String.Format("{0}://{1}/images/", context.Request.Url.Scheme, context.Request.Url.Host);
            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {
                return CouponWebservice_BUS.FindCoupons(productName, startDate, endDate, minPrice, maxPrice, path);
            }

            return null;

        }

        ////[WebMethod(MessageName = "FindCoupons_2",Description="Tim coupon truyen vao mang cac tham so",EnableSession=true)]
        ////public Coupon[] FindCoupons(string sid, string[] parameterNames, string[] parameterValues)
        ////{
        ////    return null;
        ////}

        [WebMethod]
        public Coupon FindCouponInfo(string sid, string couponId)
        {
            
            //get host name
            string path = "";
            HttpContext context = HttpContext.Current;
            path = String.Format("{0}://{1}/images/", context.Request.Url.Scheme, context.Request.Url.Host);
          
           
            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {
                return CouponWebservice_BUS.FindCouponInfo(couponId, path);
            }

            return null;

        }

        [WebMethod]
        public bool RegisterCoupon(string sid, string couponId)
        {
            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {
                return CouponWebservice_BUS.RegisterCoupon(sid, couponId);
            }

            return false;

        }

        [WebMethod]
        public string OrderCoupon(string sid, string couponId, int count)
        {
            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {
                string validationCode = CouponWebservice_BUS.OrderCoupon(sid, couponId, count);
                return validationCode;
            }

            return ErrorCode.UnSuccessfulAuthenticateMsg;

        }

        [WebMethod]
        public bool ValidateOrder(string sid, string validationCode)
        {
            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {


                bool kq = CouponWebservice_BUS.ValidateOrder(sid, validationCode);
                if (kq == false)
                {
                    return false;
                }
                return true;

            }
            return false;
        }

        //[WebMethod]
        //public string ValidateOrder(string sid, string validationCode)
        //{
        //    if (CouponWebservice_BUS.CheckSid(sid) == true)
        //    {
        //        string kq = CouponWebservice_BUS.ValidateOrder(sid, validationCode);
        //        return kq;
        //    }
        //    return "";
        //}

        [WebMethod]
        public bool CancelOrder(string sid, string validationCode)
        {
            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {
                return CouponWebservice_BUS.CancelOrder(validationCode);
            }

            return false;

        }

        [WebMethod]
        public TMDT_Nhom1A.TransportWS.TransportCompany[] GetTranports()
        {
            return CouponWebservice_BUS.GetTranports();
        }

        //[WebMethod]
        //public string ConfirmOrder(string sid, string transCompanyId, string transTypeId, string validationCode, ContactInfo contact)
        //{
        //    if (CouponWebservice_BUS.CheckSid(sid) == true)
        //    {
        //        return CouponWebservice_BUS.ConfirmOrder(transCompanyId, transTypeId, validationCode, contact);
        //    }
        //    return String.Empty;
        //}

        [WebMethod]
        public string ConfirmOrder(string sid, string transCompanyId, string transTypeId, string validationCode, ContactInfo contact)
        {
            if (CouponWebservice_BUS.CheckSid(sid) == true)
            {               
                return CouponWebservice_BUS.ConfirmOrder(transCompanyId, transTypeId, validationCode, contact);
            }
            return ErrorCode.UnSuccessfulAuthenticateMsg;
        }

        //[WebMethod]
        //public string ConfirmOrder(string sid, string transCompanyId, string transTypeId, string validationCode)
        //{
        //    ContactInfo contact = new ContactInfo();
        //    contact.SoDienThoai = "14143124134";
        //    contact.Email = "doanlang.mh@gmail.com";
        //    contact.DiaChi = "duong 12, quan thu duc, Hà Nội";
        //    return CouponWebservice_BUS.ConfirmOrder(transCompanyId, transTypeId, validationCode, contact);
            
        //}
        

    }
}

